﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Hr.DAL
{
    /// <summary>
    /// 数据访问类Base_RolePermitionRelationInfo。
    /// </summary>
    public class Base_RolePermitionRelationInfo
    {
        public Base_RolePermitionRelationInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Base_RolePermitionRelationInfo where RecordId=@RecordId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 复制角色
        /// </summary>
        public static void Copy(Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine model, string roleid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("insert into Base_RolePermitionRelationInfo select newid(),'{0}',PermitId from Base_RolePermitionRelationInfo where RoleId='{1}';", model.RoleId.ToString(), roleid);
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            db.ExecuteNonQuery(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Hr.Entity.Base_RolePermitionRelationInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Base_RolePermitionRelationInfo(");
            strSql.Append("RecordId,RoleId,PermitId)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@RoleId,@PermitId)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "RoleId", DbType.Guid, model.RoleId);
            db.AddInParameter(dbCommand, "PermitId", DbType.Guid, model.PermitId);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Hr.Entity.Base_RolePermitionRelationInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Base_RolePermitionRelationInfo set ");
            strSql.Append("RoleId=@RoleId,");
            strSql.Append("PermitId=@PermitId");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "RoleId", DbType.Guid, model.RoleId);
            db.AddInParameter(dbCommand, "PermitId", DbType.Guid, model.PermitId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Base_RolePermitionRelationInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void DeleteByRoleId(Guid RoleId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Base_RolePermitionRelationInfo ");
            strSql.Append(" where RoleId=@RoleId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RoleId", DbType.Guid, RoleId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Base_RolePermitionRelationInfo GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,RoleId,PermitId from Base_RolePermitionRelationInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Hr.Entity.Base_RolePermitionRelationInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,RoleId,PermitId ");
            strSql.Append(" FROM Base_RolePermitionRelationInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "Base_RolePermitionRelationInfo");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Hr.Entity.Base_RolePermitionRelationInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,RoleId,PermitId ");
            strSql.Append(" FROM Base_RolePermitionRelationInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Hr.Entity.Base_RolePermitionRelationInfo> list = new List<Chenxin.Lib.Hr.Entity.Base_RolePermitionRelationInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Base_RolePermitionRelationInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Hr.Entity.Base_RolePermitionRelationInfo model = new Chenxin.Lib.Hr.Entity.Base_RolePermitionRelationInfo();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["RoleId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RoleId = new Guid(ojb.ToString());
            }
            ojb = dataReader["PermitId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PermitId = new Guid(ojb.ToString());
            }
            return model;
        }

        #endregion  成员方法
    }     
}
